<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>许飞一个梦想后台管理系统</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="stylesheet" href="bootstrap.min.css">
    <style>
    body {
        font-family: "微软雅黑"
    }
    
    th,
    td {
        text-align: center;
    }
    
    .list-group-item a {
        margin-left: 5px;
    }
    img{
        width: 100px;
        height: 100px;
        border-radius: 50px;
    }
    </style>
</head>

<body>
    <div class="container">
        <h2>后台管理</h2>
        <hr>
        <button type="button" class="btn btn-success" onclick="method5('tab')">将表格用Excel表格导出</button>
        <br>
        <br>
        <ul class="list-group" id="ul">
              
        </ul>
        <table class="table table-bordered" id="tab">
            <tr>
                <th>序号</th>
                <th>昵称</th>
                <th>微信</th>
                <th>手机</th>
                <th>人数</th>
                <th>管理</th>
            </tr>
        </table>
        <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#layer">清空表格信息</button>
        <!-- 模态框 -->
        <div role="dialog" class="modal fade" id="layer">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">
                            <span>&times;</span>
                        </button>
                        <h4 class="modal-title" v-show="nowIndex==-2">确认删除全部吗？</h4>
                    </div>
                    <div class="modal-body text-right">
                        <button class="btn btn-danger btn-sm" onclick="removeall()" data-dismiss="modal">确认</button>
                        <button class="btn btn-primary btn-sm" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="jquery-1.12.4.js"></script>
    <script src="bootstrap.min.js"></script>
    <script>
    var url = "https://shenyuxf.top/"

    // 从数据库请求标题
    $.ajax({
        url: `${url}title`,
        type: 'GET',
        success: (data) => {
            $('<div>').html(`
                         <li class="list-group-item"><h4>当前logo</h4></li>                   
                         <li class="list-group-item">
                         <img src="${url}1.jpg" alt="" />

                
                         <a href="xglogo.html" class="btn btn-info" >修改</a> </li>

                           <li class="list-group-item"><h4>当前标题</h4></li>                   
                         <li class="list-group-item">

                         ${data[0].title}
                         <a href="xgtitle.html?_id=${data[0]._id}" class="btn btn-info" >修改</a> </li>

            `).appendTo("#ul")

        },
        dataType: 'json',
        error: (xhr, status, error) => {
            console.log(status)
        }
    })

    $.ajax({
            url: url + 'myCount',
            type: 'GET',
            success: (data) => {

                $.each(data, function(index, val) {

                    // console.log(val)
                    $("<tbody>").html(`
						<tr>
							<td>${index+1}</td>
							<td>${val.name}</td>
                             <td>${val.weixin}</td>
					         <td>${val.phone}</td>
							 <td>${val.num}</td>
							 <td><a href="xg.html?_id=${val._id}" type="button" class="btn btn-info">操作</a>						
						</tr>
						
					`).appendTo('#tab');

                })

            },
            dataType: 'json',
            error: (xhr, status, error) => {

            }
        })
        //清空表格数据
    function removeall() {
        $.ajax({
            url: url + 'removeall',
            type: 'GET',
            success: (data) => {
                console.log(data)
                alert("全部删除成功")
                window.location = "index.html"


            },
            dataType: 'text',
            error: (xhr, status, error) => {

            }
        })

    }






    var idTmr;

    function getExplorer() {
        var explorer = window.navigator.userAgent;
        //ie  
        if (explorer.indexOf("MSIE") >= 0) {
            return 'ie';
        }
        //firefox  
        else if (explorer.indexOf("Firefox") >= 0) {
            return 'Firefox';
        }
        //Chrome  
        else if (explorer.indexOf("Chrome") >= 0) {
            return 'Chrome';
        }
        //Opera  
        else if (explorer.indexOf("Opera") >= 0) {
            return 'Opera';
        }
        //Safari  
        else if (explorer.indexOf("Safari") >= 0) {
            return 'Safari';
        }
    }

    function method5(tableid) {
        if (getExplorer() == 'ie') {
            var curTbl = document.getElementById(tableid);
            var oXL = new ActiveXObject("Excel.Application");
            var oWB = oXL.Workbooks.Add();
            var xlsheet = oWB.Worksheets(1);
            var sel = document.body.createTextRange();
            sel.moveToElementText(curTbl);
            sel.select();
            sel.execCommand("Copy");
            xlsheet.Paste();
            oXL.Visible = true;

            try {
                var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
            } catch (e) {
                print("Nested catch caught " + e);
            } finally {
                oWB.SaveAs(fname);
                oWB.Close(savechanges = false);
                oXL.Quit();
                oXL = null;
                idTmr = window.setInterval("Cleanup();", 1);
            }

        } else {
            tableToExcel(tableid)
        }
    }

    function Cleanup() {
        window.clearInterval(idTmr);
        CollectGarbage();
    }
    var tableToExcel = (function() {
        var uri = 'data:application/vnd.ms-excel;base64,',
            template = '<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>',
            base64 = function(s) {
                return window.btoa(unescape(encodeURIComponent(s)))
            },
            format = function(s, c) {
                return s.replace(/{(\w+)}/g,
                    function(m, p) {
                        return c[p];
                    })
            }
        return function(table, name) {
            if (!table.nodeType) table = document.getElementById(table)
            var ctx = {
                worksheet: name || 'Worksheet',
                table: table.innerHTML
            }
            window.location.href = uri + base64(format(template, ctx))
        }
    })()
    </script>
</body>

</html>
